package cn.abyss.admin.util;

import java.io.Serializable;
import java.util.*;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2021/8/25
 */
public class ResultMap implements Map, Serializable {
    private Map map = new HashMap();

    public ResultMap() {
    }

    public int size() {
        return this.map.size();
    }

    public boolean isEmpty() {
        return this.map.isEmpty();
    }

    public boolean containsKey(Object key) {
        return this.map.containsKey(this.toLowerCase(key));
    }

    private Object toLowerCase(Object key) {
        if (key != null && key instanceof String) {
            key = ((String)key).toLowerCase();
        }

        return key;
    }

    public boolean containsValue(Object value) {
        return this.map.containsValue(value);
    }

    public Object get(Object key) {
        return this.map.get(this.toLowerCase(key));
    }

    public Object put(Object key, Object value) {
        return this.map.put(this.toLowerCase(key), value);
    }

    public Object remove(Object key) {
        return this.map.remove(this.toLowerCase(key));
    }

    public void putAll(Map m) {
        Iterator i = m.entrySet().iterator();

        while(i.hasNext()) {
            Entry e = (Entry)i.next();
            this.put(e.getKey(), e.getValue());
        }

    }

    public void clear() {
        this.map.clear();
    }

    public Set keySet() {
        return this.map.keySet();
    }

    public Collection values() {
        return this.map.values();
    }

    public Set entrySet() {
        return this.map.entrySet();
    }

    public String toString() {
        return this.map.toString();
    }

    public String getAttribute(String name) {
        return this.getString(name);
    }

    public void setAttribute(String name, String value) {
        this.put(name, value);
    }

    public Object get(Object key, Object defaultValue) {
        Object result = this.get(key);
        if (result == null || result.equals("")) {
            result = defaultValue;
        }

        return result;
    }

    public String getString(Object key) {
        Object result = this.get(key);
        return result == null ? null : result.toString().trim();
    }

    public String getString(Object key, String defaultValue) {
        Object result = this.get(key, defaultValue);
        return result == null ? null : result.toString().trim();
    }

    public int getInt(Object key) {
        return (int)Double.parseDouble(this.getString(key));
    }

    public int getInt(Object key, int defaultValue) {
        String result = this.getString(key, String.valueOf(defaultValue));
        if (result.length() < 1) {
            result = "0";
        }

        return (int)Double.parseDouble(result);
    }

    public long getLong(Object key) {
        return (long)Double.parseDouble(this.getString(key));
    }

    public long getLong(Object key, long defaultValue) {
        String result = this.getString(key, String.valueOf(defaultValue));
        if (result.length() < 1) {
            result = "0";
        }

        return (long)Double.parseDouble(result);
    }

    public double getDouble(Object key) {
        return Double.parseDouble(this.getString(key));
    }

    public double getDouble(Object key, double defaultValue) {
        return Double.parseDouble(this.getString(key, String.valueOf(defaultValue)));
    }
}

